import React, { useState } from 'react';
import CommonPage from '@/components/page/CommonPage';
import { format } from '@/utils/datetime';
const daoName = 'ShJhcwJxffYjjx';
const importName = 'ShJhcwJxffYjjx';
const columns: API.Column[] = [
  {
    title: '员工号',
    width: 110,
    sorter: true,
    dataIndex: 'EMNO',
    key: 'EMNO',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '员工名',
    width: 110,
    sorter: true,
    dataIndex: 'EMNM',
    key: 'EMNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '部门',
    width: 110,
    sorter: true,
    key: 'DPNM',
    dataIndex: 'DPNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '绩效金额（含岗位津贴）',
    width: 110,
    sorter: true,
    key: 'JXJE',
    dataIndex: 'JXJE',
    dispaly: true,
    filter: true,
    export: true,
  },
];
const controls: API.Control[] = [
  { name: 'EMNO', type: 'text', label: '员工号', grid: 12 / 24 },
  { name: 'EMNM', type: 'text', label: '员工名', grid: 12 / 24 },
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },

  {
    name: 'JXJE',
    rules: [{ required: true }],
    type: 'number',
    label: '绩效金额（含岗位津贴）',
    grid: 12 / 24,
    dataType: 'number',
  },
];

const controlsImport: API.Control[] = [
  { name: 'EMNO', type: 'text', label: '员工号', grid: 12 / 24 },
  { name: 'EMNM', type: 'text', label: '员工名', grid: 12 / 24 },
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },
  {
    name: 'JXJE',
    rules: [{ required: true }],
    type: 'number',
    label: '绩效金额（含岗位津贴）',
    grid: 12 / 24,
    dataType: 'number',
  },
];

const ShJhcwJxffYjjx: React.FC = () => {
  const initParams = (curDate: Date) => {
    let drfs = 2;
    const montIndex = curDate.getMonth();
    const currMonth = montIndex + 1;
    let date = new Date(curDate.getFullYear(), montIndex, 1);
    if (currMonth == 3 || currMonth == 6 || currMonth == 9 || currMonth == 12) {
      drfs = 1;
      date = new Date(curDate.getFullYear(), currMonth, 0);
    }
    return { drfs, date: format(date, 'yyyy-MM-dd') };
  };
  const [selDate, setSelDate] = useState<string>(() => initParams(new Date()).date);
  const [drid, setDrid] = useState<number>(() => initParams(new Date()).drfs);
  const [zbid, setZbid] = useState<number>(1);
  const setParas = (row: Record<string, any>) => {
    const vals = row.FFJD.split('-');
    let dstr = row.FFJD;
    if (row.DRID === '1') {
      if (vals[1] === 'Q1' || vals[1] === '01' || vals[1] === '02' || vals[1] === '03') {
        dstr = vals[0] + '-03-31';
      } else if (vals[1] === 'Q2' || vals[1] === '04' || vals[1] === '05' || vals[1] === '06') {
        dstr = vals[0] + '-06-30';
      } else if (vals[1] === 'Q3' || vals[1] === '07' || vals[1] === '08' || vals[1] === '09') {
        dstr = vals[0] + '-09-30';
      } else if (vals[1] === 'Q4' || vals[1] === '10' || vals[1] === '11' || vals[1] === '12') {
        dstr = vals[0] + '-12-31';
      }
    } else {
      dstr = dstr.substr(0, 7) + '-' + '01';
    }
    setSelDate(dstr);
    setZbid(row.ZBID);
  };
  const updrfs = (value: string) => {
    if (value == '1') {
      setDrid(1);
    } else if (value == '2') {
      setDrid(2);
    }
  };
  if (selDate == undefined) {
    return <></>;
  }
  const scontrols: API.Control[] = [
    {
      name: 'DRID',
      label: '导入方式',
      type: 'select',
      width: 120,
      grid: 12 / 24,
      rules: [{ required: true, message: '导入方式不能为空' }],
      dataType: 'number',
      initValue: '' + drid,
      options: [
        { value: '1', label: '季度' },
        { value: '2', label: '月份' },
      ],
      onChange: async (val) => {
        updrfs(val);
      },
    },
    {
      name: 'FFJD',
      dataType: 'date',
      type: drid === 1 ? 'quarter' : 'month',
      label: '选择' + (drid === 1 ? '季度' : '月份'),
      rules: [{ required: true, message: (drid === 1 ? '季度' : '月份') + '不能为空' }],
      width: 120,
      initValue: selDate,
    },
    {
      name: 'ZBID',
      label: '选择条线',
      type: 'select',
      width: 180,
      grid: 12 / 24,
      rules: [{ required: true, message: '条线不能为空' }],
      dataType: 'number',
      initValue: zbid,
      fetch: {
        tableName: 'Jxffzb',
        optionTpl: 'ZBMC',
        valueTpl: 'ZBID',
        isDao: true,
      },
    },
  ];
  return (
    <CommonPage
      daoName={daoName}
      columns={columns}
      controls={controls}
      title="业绩绩效"
      importName={importName}
      importControls={controlsImport}
      importParamNames={['FFJD', 'ZBID']}
      importMode="DeleteAfterInsert"
      keyColName="ID"
      importParamVals={{
        FFJD: selDate,
        DRID: '' + drid,
        ZBID: '' + zbid,
      }}
      importTemplateName="业绩绩效导入模板"
      onShortcutChange={async (vals) => setParas(vals)}
      shortcutFilters={{
        controls: scontrols,
      }}
    />
  );
};
export default ShJhcwJxffYjjx;
